package com.sunday.volunteer.ui.mine;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.event.EventBus;
import com.sunday.common.imageselector.MultiImageSelectorActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ImageUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.member.base.BaseActivity;
import com.sunday.member.common.MemberConst;
import com.sunday.member.event.ExitApp;
import com.sunday.member.utils.SharePerferenceUtils;
import com.sunday.member.utils.UploadUtils;
import com.sunday.volunteer.BaseApp;
import com.sunday.volunteer.R;
import com.sunday.volunteer.entity.Images;
import com.sunday.volunteer.entity.Member;
import com.sunday.volunteer.http.HttpClient;
import com.sunday.volunteer.ui.EditTextActivity;
import com.sunday.volunteer.ui.MainActivity;
import com.sunday.volunteer.ui.user.LoginActivity;
import com.sunday.volunteer.ui.user.SendCodeActivity;
import com.sunday.volunteer.utils.BaseUtil;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by admin on 2017/1/13.
 */

public class UserInfoActivity extends BaseActivity{

    @Bind(R.id.title_text)
    TextView title_text;
    @Bind(R.id.logo)
    ImageView logo;
    @Bind(R.id.name)
    TextView name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        ButterKnife.bind(this);
        title_text.setText("我的资料");
        getInfo();
    }

    @OnClick(R.id.update_logo)
    void updateLogo(){
        toImage();
    }

    @OnClick(R.id.update_name)
    void updateName(){
        intent = new Intent(mContext , EditTextActivity.class);
        intent.putExtra(BaseUtil.TITLE, "姓名");
        intent.putExtra(BaseUtil.CONTENT, name.getText().toString());
        startActivityForResult(intent , 2222);
    }


    @OnClick(R.id.update_password)
    void updatePassword(){
        BaseUtil.openSendCodeActivity(mContext , 1);
    }


    //退出登录
    @OnClick(R.id.next_btn)
    void nextBtn(){
        BaseUtil.showDialog(mContext, "是否注销登录", new BaseUtil.BaseUtilClickListener() {
            @Override
            public void doRightbtn() {
                SharePerferenceUtils.getIns(mContext).putBoolean(MemberConst.IS_LOGIN , false);
                SharePerferenceUtils.getIns(mContext).putBoolean("is_loginSelf", false);
                EventBus.getDefault().post(new ExitApp());
                BaseUtil.openActivity(mContext, LoginActivity.class);
            }
        });
    }

    private void getInfo(){
        Call<ResultDO<Member>> call = HttpClient.getHttpAdapter().getMemberDeatail(BaseApp.getInstance().getMemberId());
        call.enqueue(new Callback<ResultDO<Member>>() {
            @Override
            public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                if(response.body() == null || isFinish ){
                    return;
                }
                if(response.body().getCode() == 0 && response.body().getResult()!= null){
                    Member member = response.body().getResult();
                    setData(member);
                }else {
                    ToastUtils.showToast(mContext , response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Member>> call, Throwable t) {

            }
        });
    }

    private void setData(Member member){

        Picasso.with(mContext)
                .load(TextUtils.isEmpty(member.getLogo())?"http":member.getLogo())
                .placeholder(R.drawable.default_portrait)
                .centerCrop()
                .resize(180,180)
                .into(logo);
        name.setText(TextUtils.isEmpty(member.getName())?"昵称":member.getName());
    }


    public void toImage(){
        intent = new Intent(mContext, MultiImageSelectorActivity.class);
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 1);
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_SINGLE);
        startActivityForResult(intent, 1111);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 1111:
                    showLoadingDialog(R.string.upload_img);
                    final ArrayList<String> mSelectPath = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
                    final String imgPath = ImageUtils.getCompressImagePath(mSelectPath.get(0));

                    Call<ResultDO<Images>> call =  HttpClient.getHttpAdapter().uploadSingleFile(UploadUtils.getRequestBody(imgPath));
                    call.enqueue(new Callback<ResultDO<Images>>() {
                        @Override
                        public void onResponse(Call<ResultDO<Images>> call, Response<ResultDO<Images>> response) {
                            dissMissDialog();
                            if (response.body() == null) {
                                return;
                            }
                            ResultDO<Images> resultDO = response.body();
                            if (resultDO.getCode() == 0) {
                                String imagePath = resultDO.getResult().getHalfPath();
                                update(null  , imagePath);
                            }else {
                                ToastUtils.showToast(mContext , "图片上传失败");
                            }
                        }

                        @Override
                        public void onFailure(Call<ResultDO<Images>> call, Throwable t) {
                            dissMissDialog();
                        }
                    });
                    //上传图片
                    return;

                case 2222:
                    if(data.getExtras() != null){
                        String name = data.getStringExtra(BaseUtil.CONTENT);
                        update(name  , null);
                    }
                    break;
            }
        }
    }

    private void update(String name  ,String logo){
        Call<ResultDO<Member>> call = HttpClient.getHttpAdapter().updateMember(BaseApp.getInstance().getMemberId() , name , logo);
        call.enqueue(new Callback<ResultDO<Member>>() {
            @Override
            public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                if(response.body() == null || isFinish){
                    return;
                }
                if(response.body().getCode() == 0 && response.body().getResult() != null){
                    SharePerferenceUtils.getIns(mContext).saveOAuth(response.body().getResult());
                    setData(response.body().getResult());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Member>> call, Throwable t) {

            }
        });
    }


}
